package dxn.nl.file.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import dxn.boot.common.entity.CsBaseCodeNameDto;
import dxn.boot.common.entity.CsBasePageQuery;
import dxn.boot.common.entity.CsBasePageResult;
import dxn.nl.file.biz.dao.AppModeUserBindDao;
import dxn.nl.file.biz.dao.AppModelDao;
import dxn.nl.file.biz.po.AppModeUserBindPo;
import dxn.nl.file.biz.po.AppModelPo;
import dxn.nl.file.biz.vo.CsExportModelUserBindVo;
import dxn.nl.file.service.AppModeUserBindService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AppModeUserBindServiceImpl extends ServiceImpl<AppModeUserBindDao,AppModeUserBindPo> implements AppModeUserBindService {
    @Resource
    private AppModelDao appModelDao;

    @Override
    public String userBindMode(CsExportModelUserBindVo vo) {
        return null;
    }

    @Override
    public String userUnbindMode(CsExportModelUserBindVo vo) {
        return null;
    }

    @Override
    public String modeBindUser(CsExportModelUserBindVo vo) {
        LambdaQueryWrapper<AppModeUserBindPo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AppModeUserBindPo::getModeCode, vo.getModeCode())
                .eq(AppModeUserBindPo::getExportTypeCode, vo.getExportTypeCode())
                .in(AppModeUserBindPo::getCode, vo.getMemberList());

        List<AppModeUserBindPo> existCodeObjs = this.baseMapper.selectList(lambdaQueryWrapper);
        if(!CollectionUtils.isEmpty(existCodeObjs))
        {
            List<String> userCodeList = existCodeObjs.stream().map(c->c.getCode()).collect(Collectors.toList());
            vo.getMemberList().removeAll(userCodeList);
        }
        if(CollectionUtils.isEmpty(vo.getMemberList()))
        {
            return null;
        }

        List<AppModeUserBindPo> list = new ArrayList<>();
        for(String code:vo.getMemberList())
        {
            AppModeUserBindPo appModeUserBindPo = new AppModeUserBindPo();
            appModeUserBindPo.setCode(code);
            appModeUserBindPo.setModeCode(vo.getModeCode());
            list.add(appModeUserBindPo);
        }

        this.saveBatch(list);
        return null;
    }

    @Override
    public String modeUnBindUser(CsExportModelUserBindVo vo) {
        LambdaQueryWrapper<AppModeUserBindPo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AppModeUserBindPo::getModeCode, vo.getModeCode())
                .in(AppModeUserBindPo::getCode, vo.getMemberList());
        this.baseMapper.delete(lambdaQueryWrapper);
        return null;
    }

    @Override
    public CsBasePageResult<CsBaseCodeNameDto> pageModeBindUser(CsBasePageQuery<CsExportModelUserBindVo> pageQuery) {
        return null;
    }

    @Override
    public CsBasePageResult<CsBaseCodeNameDto> pageModeUnBindUser(CsBasePageQuery<CsExportModelUserBindVo> pageQuery) {
        return null;
    }
}
